package com.zcx.project.controller;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.Part;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zcx.atom.util.DateFormatter;
import com.zcx.atom.util.UserUtil;
import com.zcx.project.entity.ZcxDepartment;
import com.zcx.project.entity.ZcxPlan;
import com.zcx.project.entity.ZcxPlanTask;
import com.zcx.project.entity.ZcxUser;
import com.zcx.project.mapper.ZcxDepartmentMapper;
import com.zcx.project.mapper.ZcxPlanMapper;
import com.zcx.project.mapper.ZcxPlanTaskMapper;


@Controller
@RequestMapping("/ZcxPlanTask")
public class ZcxPlanTaskController {

    @Autowired
    ZcxPlanTaskMapper zcxPlanTaskMapper;
    @Autowired
    ZcxDepartmentMapper zcxDepartmentMapper;

    @RequestMapping(value = "ZcxPlanTaskHtml")
    public ModelAndView getMainPage(HttpSession session, HttpServletRequest request) {
        ModelAndView mv = new ModelAndView("ZcxPlanTask");
        return mv;
    }

    @ResponseBody
    @RequestMapping(value = "/findList")
    public List findList(ZcxPlanTask obj) {
        List list = zcxPlanTaskMapper.findList(obj);
        return list;
    }

    @ResponseBody
    @RequestMapping(value = "/query")
    public Map<String, Object> findZcxPlanPage(int currentPage, int rows, ZcxPlanTask obj, HttpSession session) {
        ZcxUser currentUser = (ZcxUser) session.getAttribute("user");
        //使用分页插件,核心代码就这一行，页数、每页行数
        PageHelper.startPage(currentPage, rows);
        List<ZcxPlanTask> list;
        if (11 == currentUser.getJobId()) {
            list = zcxPlanTaskMapper.findList(obj);
        } else {
            obj.setPlanFzr(currentUser.getName());
            list = zcxPlanTaskMapper.findList(obj);
        }

        PageInfo<ZcxPlanTask> pageInfo = new PageInfo<ZcxPlanTask>(list);
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("total", pageInfo.getTotal());
        result.put("rows", pageInfo.getList());
        return result;


    }


    @ResponseBody
    @RequestMapping(value = "/insertZcxPlanTask")
    public Map<String, Object> addZcxPlanTask(ZcxPlanTask obj, HttpServletRequest request) throws ServletException, IOException {
        HttpSession session = request.getSession();
        ZcxUser currentUser = (ZcxUser) session.getAttribute("user");
        obj.setPlanFzr(currentUser.getName());
        ZcxDepartment zcxDepartment = zcxDepartmentMapper.selectByPrimaryKey(currentUser.getDepartmentId());
        obj.setPlanCj(zcxDepartment.getDepartmentName());
        obj.setCreateTime(DateFormatter.formatDate("", new Date()));
        int i = zcxPlanTaskMapper.insertSelective(obj);

//        request.getfi
        Map<String, Object> returnMap = new HashMap<String, Object>();
        if (i == 0) {
            returnMap.put("showInfo", "添加失败！");
        } else {
            returnMap.put("showInfo", "添加成功！");
            returnMap.put("data", obj);
        }

        return returnMap;
    }

    @ResponseBody
    @RequestMapping(value = "/tj")
    public Map<String, Object> tj(ZcxPlanTask obj, HttpServletRequest request) {
        int i = zcxPlanTaskMapper.updateByPrimaryKeySelective(obj);
        Map<String, Object> returnMap = new HashMap<String, Object>();
        if (i == 0)
            returnMap.put("showInfo", "提交失败！");
        else
            returnMap.put("showInfo", "提交成功！");
        return returnMap;
    }

    @ResponseBody
    @RequestMapping(value = "/updateZcxPlanTask")
    public Map<String, Object> updateZcxPlanTask(ZcxPlanTask obj, HttpServletRequest request) {
        int i = zcxPlanTaskMapper.updateByPrimaryKeySelective(obj);
        Map<String, Object> returnMap = new HashMap<String, Object>();
        if (i == 0)
            returnMap.put("showInfo", "修改失败！");
        else
            returnMap.put("showInfo", "修改成功！");
        return returnMap;
    }

    @ResponseBody
    @RequestMapping(value = "/deleteZcxPlanTask")
    public Map<String, Object> deleteZcxPlanTask(int id) {
        int i = zcxPlanTaskMapper.deleteByPrimaryKey(id);
        Map<String, Object> returnMap = new HashMap<String, Object>();
        if (i == 0)
            returnMap.put("showInfo", "删除失败！");
        else
            returnMap.put("showInfo", "删除成功！");
        return returnMap;
    }


}
